﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Utils;
using XNADota.GamePlay;
using XNADota.Primitive;
#endregion

namespace JiggleGame.PhysicObjects
{
    public class HeightmapObject : PhysicObject
    {
        public HeightmapObject(Game game, Model model, Vector2 shift)
            : base(game, model)
        {
            body = new Body(); // just a dummy. The PhysicObject uses its position to get the draw pos
            collision = new CollisionSkin(null);

            PathManager pathManager = GameplayWorld.Instance.PathManager;
            Array2D field = new Array2D(pathManager.Width + 1, pathManager.Height + 1);

            for (int x = 0; x < pathManager.Width + 1; x++)
            {
                for (int z = 0; z < pathManager.Height + 1; z++)
                {
                    field.SetAt(x, z, pathManager.GetHeight(x, z).Y);
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class should know where to
            // draw the model.
            body.MoveTo(new Vector3(shift.X, 0, shift.Y), Matrix.Identity);

            collision.AddPrimitive(new Heightmap(field, shift.X, shift.Y, 32.0f, 32.0f), new MaterialProperties(0.7f, 0.7f, 0.6f));

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(collision);
        }

        public override void ApplyEffects(BasicEffect effect)
        {
            effect.SpecularColor = new Vector3(0.2f, 0.2f, 0.2f);
            effect.PreferPerPixelLighting = true;
        }
    }
}
